<template>
	<view>
		<view class="content">
			<!-- <u-cell title="议题模板" :isLink="true" :value="content_type" style='border:0' @click="opentopic_template">
			</u-cell> -->
			<view class="file-box content-box time" v-if="meetDetails.is_faqi===1" @click="openTime">
				{{timeData}}
			</view>
			<!-- <view class="">
				<u--textarea v-model="content" placeholder="请输入议题内容" height="150" confirmType="done"></u--textarea>
			</view> -->
			<!-- <view class="img-box">
			<u-upload :fileList="fileList" @afterRead="afterRead" @delete="deletePic" name="1" multiple :maxCount="8"
				width="105" height="105" uploadText="添加图片" uploadIcon="plus" :previewFullImage="true">
			</u-upload>
		</view> -->
		
<!-- 			<view class="file-box content-box" v-if="meetDetails.is_faqi!==1">
				<u-upload :fileList="fileList1" :previewImage="false" @afterRead="afterRead1" @delete="deletePic1"
					accept="file" :maxCount="100" name="1" multiple uploadText="上传附件">
					<view class="content-box">
						<div style="position: relative" v-if="show_files" v-for="(item,index) in files">
							<div class="file-name" @click.stop="handleDownload(item)">
								<u-icon name="attach" color="#4871C0" size="24"></u-icon>
								<label class="file-txt">{{item.filename}}</label>
							</div>
							<view class="new-tip" @click.stop="deletePic1(item)">
								<u--image :src="delete_icon" width="40rpx" height="28rpx">
								</u--image>
							</view>
						</div>
						<view class="file-tip" @click="afterRead1">
							上传附件
						</view>
					</view>
				</u-upload>
			</view> -->

			<!-- <view class="tishititle" v-if="meetDetails.is_faqi!==1">仅支持上传word、excel、ppt、pdf、图片类型的文件</view> -->
			<view class="bottom-btn">
				<view class="button primary" @click="isLoading?'':save(1)">
					删除
				</view>
				<view class="button" @click="isLoading?'':save()">
					设置
				</view>
			</view>

		</view>
		<u-popup :show="showTemp" mode="bottom" @close="closetopic_template" @open="opentopic_template"
			:closeable="true" :border-radius="10" close-icon-color="#010101" :mask-close-able="false"
			close-icon-size="42">
			<view class="content" style="height:60%">
				<view class="title">请选择议题模板</view>
				<view @click="seltopic_template(v)" :class="select==v.id?'meet active':'meet '"
					v-for="(v, i) in topic_template">
					{{ v.title }}
					<text>
						<img src="@/static/assets/images/dui.png" v-if="select==v.id" /></text>
				</view>

				<view class="btn">
					<u-button class='cancelbtn' @click="closetopic_template">取消</u-button>
					<u-button type="primary" text="确定" @click="suretopic_template" color='#4871C0'></u-button>
				</view>
			</view>
		</u-popup>
		
		<u-picker :show='show' :columns='arrte'  mode='time' :defaultIndex='defaultIndex' @confirm='confirmPicker' @cancel='show=false' @columnchange='columnchangePricker'></u-picker>

	</view>
</template>

<script>
	import {
		HTTP_REQUEST_URL,
	} from '@/config/app';
	import {
		addTopic,
		getMeetDetails,
		meet_topic_template,
		setPlanOffTime
	} from '@/api/meetApi.js'
	export default {
		data() {
			return {
				arrte:[[],[],[],[],[]],
				defaultIndex:[],
				dataTime:'',
				timeData:'工作计划截至时间',
				show:false,
				delete_icon: require('@/static/delete.png'),
				id: '',
				content: '',
				content_type: '',
				fileList: [],
				fileList1: [],
				images: [],
				files: [],
				fileName: null,
				show_files: false,
				form: {},
				showTemp: false,
				topic_template: [],
				select: 0,
				select_topic: {},
				isLoading: false,
				meetDetails:{
					
				},
				
			
			}
		},
		onLoad(options) {
			this.getmeet_topic_template();
			this.id = options.id
			this.getData()
			let date = new Date();
			let nowYear = date.getFullYear();
			let nowMonth = date.getMonth() + 1;
			let d = new Date(2022, nowMonth, 0);
			
			for(let i = 1999; i <= nowYear; i++){
				this.arrte[0].push(i+'年');
			}
			this.defaultIndex[0] = (this.arrte[0].length-1);
		
			for(let i = 1; i <= 12;i++){
				if(i == nowMonth-1) {
					this.defaultIndex[1] = i;
				}
				this.arrte[1].push(i+'月');
			}
			
			
			for(let i = 1; i <= d.getDate(); i++){
				if(i == date.getDate()-1) {
					this.defaultIndex[2] = i;
				}
				this.arrte[2].push(i+'日');
			}
			for(let i = 0; i <= 23; i++){
				if(i == date.getHours()) {
					this.defaultIndex[3] = i;
				}
				this.arrte[3].push(i+'时');
			}
			for(let i = 0; i <= 59; i++){
				if(i == date.getMinutes()) {
					this.defaultIndex[4] = i;
				}
				this.arrte[4].push(i+'分');
			}
		},
		methods: {
			columnchangePricker(){
				console.log(55);
			},
			confirmPicker(e){
				console.log(e);
				let index = e.value.length;
			
				let time = []
				for(let i = 0; i < index;i++){
					time.push(parseInt(e.value[i]))
				}
				this.timeData = `${time[0]}-${time[1]<10?'0'+time[1]:time[1]}-${time[2]<10?'0'+time[2]:time[2]} ${time[3]<10?'0'+time[3]:time[3]}:${time[4]<10?'0'+time[4]:time[4]}:00`
				console.log(this.timeData);
				this.show = false
			},

			openTime(){
				this.show = true
			},
			getData() {
				this.isLoading = true;
				let params = {
					id: this.id
				};
				getMeetDetails(params).then(res => {
					this.isLoading = false;
			
					this.meetDetails = res.data
					if(res.data.plan_off_time_text){
						this.timeData = res.data.plan_off_time_text;
					}
					
					console.log("getMeetDetails数据", res, this.dataInfo)
				})
			},
			// 获取议题模板
			getmeet_topic_template() {
				meet_topic_template().then(res => {
					console.log(res)
					if (res.code == 1) {
						this.topic_template = res.data
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 1000
						});
					}

				})
			},
			opentopic_template() {
				this.showTemp = true;
			},
			closetopic_template() {
				this.showTemp = false;
			},
			suretopic_template() {
				this.showTemp = false;
				this.content = this.select_topic.content;
				this.content_type = this.select_topic.title;
			},

			seltopic_template(item) {
				this.select_topic = item
				this.select = item.id;
			},
			// 点击附件预览
			handleDownload(item) {
				console.log("=====item", item)
				let params = {
					url: item.fullurl, // 需要预览文件的地址(必填，可以使用相对路径)
					name: item.filename, // 需要预览文件的文件名(不填的话取url的最后部分)
					size: item.filesize, // 需要预览文件的字节大小(必填)
					type: item.mimetype
				}
				this.$store.dispatch('yulan', params)
			},
			// 保存
			save(type) {
				
				if (!this.isLoading) {
					// if (!this.content) {
					// 	uni.showToast({
					// 		title: '请填写议题内容',
					// 		icon: 'none',
					// 		duration: 1000
					// 	});
					// 	return;
					// }
					let time = this.timeData;
					if(type!==1){
						if (this.timeData =='工作计划截至时间') {
							uni.showToast({
								title: '请选择时间',
								icon: 'none',
								duration: 1000
							});
							return;
						}
					}else{
						time = ''
						this.timeData = '工作计划截至时间'
						this.content = ''
					}
					

					let params = {
						meet_id: this.id,
						content: this.content,
						// files: JSON.stringify(this.files || []),
						plan_off_time:time
					};
					this.isLoading = true;
					//显示加载框
					uni.showLoading({
						title: '正在提交'
					})
					setPlanOffTime(params).then(res => {
						console.log(res)
						if (res.code == 1) {
							this.isLoading = false;
							uni.showToast({
								title: res.msg,
								icon: 'none',
								duration: 1000
							});
							setTimeout(function() {
								
								uni.navigateBack({
									delta: 1
								});
							
								uni.hideLoading();
							}, 1500)
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none',
								duration: 1000
							});
							this.isLoading = false;
							uni.hideLoading();
						}

					})
				}
			},
			// 删除图片
			deletePic(event) {
				this.fileList.splice(event.index, 1)
				this.images.splice(event.index, 1)
			},
			// 新增图片
			// async afterRead(event) {
			// 	// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
			// 	let lists = [].concat(event.file)
			// 	let fileListLen = this.fileList.length
			// 	lists.map((item) => {
			// 		this.fileList.push({
			// 			...item,
			// 			status: 'uploading',
			// 			message: '上传中'
			// 		})
			// 	})
			// 	for (let i = 0; i < lists.length; i++) {
			// 		const result = await this.uploadFilePromise(lists[i].url)
			// 		console.log("result：", result)
			// 		let item = this.fileList[fileListLen]
			// 		this.fileList.splice(fileListLen, 1, Object.assign(item, {
			// 			status: 'success',
			// 			message: '',
			// 			url: result
			// 		}))
			// 		this.images.push(result)
			// 		console.log("images:", this.images)
			// 		fileListLen++
			// 	}
			// },
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: HTTP_REQUEST_URL + '/api/common/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							console.log("res:", res)
							if (JSON.parse(res.data).code == 1) {
								setTimeout(() => {
									resolve(JSON.parse(res.data).data.files)
								}, 500)
							} else {
								uni.showToast({
									title: JSON.parse(res.data).msg,
									icon: 'none',
									duration: 1000
								});
							}

						}
					});
				})
			},
			// 新增文件
			async afterRead1(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [];
				if(event.file){lists=event.file}
				let fileListLen = this.fileList1.length
				lists.forEach((item) => {
					this.fileList1.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise1(lists[i].url)
					let item = this.fileList1[fileListLen]
					this.fileList1.push({
						...item,
						status: 'success',
						message: '',
					})
					this.show_files=true
					this.files.push(result)
					fileListLen++
				}
			},

			uploadFilePromise1(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: this.$httpApi + '/api/common/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'hy'
						},
						success: (res) => {
							console.log("res:", res)
							if (JSON.parse(res.data).code == 1) {
								setTimeout(() => {
									resolve(JSON.parse(res.data).data.files)
								}, 500)
							} else {
								uni.showToast({
									title: JSON.parse(res.data).msg,
									icon: 'none',
									duration: 1000
								});
							}
						}
					});
				})
			},
			deletePic1(event) {
				let _this=this
				uni.showModal({
					title: '提示',
					content: '您确定要删除吗?',
					success: function(res) {
						if (res.confirm) {
							_this.fileList1.splice(event.index, 1)
							_this.files.splice(event.index, 1)
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
				
			},

		}
	}
</script>

<style>
	@import "@/static/assets/css/index.css";
	@import "@/static/assets/css/mobiscroll.core-2.5.2.css";
	@import "@/static/assets/css/mobiscroll.animation-2.5.2.css";
	@import "@/static/assets/css/mobiscroll.android-ics-2.5.2.css";

	.tishititle {
		font-size: 20rpx;
		color: #ccc;
		padding-top: 20rpx;
	}

	.content {
		background-color: #F8F8F8;
		height: 100vh;
		padding: 30rpx;
		position: relative;
		box-sizing: border-box;
	}

	.img-box {
		margin: 30rpx 0;
	}

	/deep/.u-upload__button {
		background-color: #fff;
	}

	.file-box {
		width: 100%;
		margin-top: 20rpx;
	}

	.file-tip {
		background-color: #fff;
		padding: 30rpx;
		width: 620rpx;
		font-size: 28rpx;
		font-weight: 500;
		color: #999999;
	}

	.button {
		background: #4871C0;
		border-radius: 8rpx;
		opacity: 1;
		font-size: 28rpx;
		font-weight: 500;
		color: #FFFFFF;
		text-align: center;
		padding: 30rpx 0;	
		
	}
	.button.primary{
		background: #ff8800;
	}
	.bottom-btn{
		position: fixed;
		display: flex;
		bottom: 10rpx;
		/* left: 30rpx; */
		width: 100%;
		left: 0;
	}
	.bottom-btn .button{
		/* width: 40%; */
		flex: 1;
		margin: 0 20rpx;
	}

	/deep/.file-box .u-upload__wrap__preview {
		width: 670rpx !important;
		/* height: 88rpx!important; */
	}

	/deep/.file-box .u-upload__button .u-icon {
		display: none !important;
	}

	/deep/.file-box .u-upload__wrap__preview .u-icon {
		display: none !important;
	}

	.file-name {
		padding: 10px;
		background: #F8F9FC;
		border-radius: 8px 8px 8px 8px;
		position: relative;
	}

	.new-tip {
		position: absolute;
		right: 0px;
		top: 0;
		/* width: 30px;
		text-align: center;
		height: 16px;
		background: #FF7A00;
		border-radius: 0px 8px 0px 4px;
		font-size: 8px;
		color: #ffffff; */
	}

	.pic_user {
		width: 38px;
		height: 38px;
		margin-bottom: 20px;
		background: #F0F0F0;
		border-radius: 4px 4px 4px 4px;
		background: #F0F0F0 url('@/static/assets/images/pick_user.png') no-repeat center center;
		background-size: 40% 40%;
	}


	.file-name {
		padding: 10px;
		background: #fff;
		border-radius: 8px 8px 8px 8px;
		position: relative;
	}

	.u-flex {
		display: flex;
		align-items: center;
	}

	.file-name {
		padding: 10px;
		background: #fff;
		border-radius: 8px 8px 8px 8px;
		display: flex;
		position: relative;
		width: 86vw;
		font-size: 30rpx;
		color: #4871C0;
		/* margin-left: 3.5vw; */

	}

	.file-txt {
		width: 80%;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.content-box {
		width: calc(100vw - 60rpx);
		min-height: 92rpx;
		background-color: #fff;
		display: flex;
		align-items: center;
		color: #999999;
	    font-size: 14px;
	}
	.content-box.time{
		padding-left: 10px;
		box-sizing: border-box;
		margin-bottom: 20rpx;
	}

	.file-tip {
		background-color: #fff;
		padding: 30rpx;
		width: calc(100vw - 48pt);
		font-size: 28rpx;
		font-weight: 500;
		color: #999999;
	}
</style>
